// ============================== 导入依赖 ============================== //

import CORE from '@/CORE'
import Client from '@api/maintain/api/PlanTaskApi'

import ComClass from '@api/ComQuery'
import ListClass from '@api/maintain/model/QueryPlanTaskAssetListReq'
import OneClass from '@api/maintain/model/QueryPlanTaskAssetDetailReq'

const CLIENT = CORE.grpcCLIENT(Client)
const COM = [ComClass, 'comQuery']

// ============================== 导出远程接口 ============================== //

export default {
  // —————————————————————————————— 列表 —————————————————————————————— //

  validParam(param, state, root) {
    return root.state.one.id
  },
  transParam(param, { rootState: { one: { id } } }) {
    return { ...param, planTaskId: id }
  },
  async getList(param, TIP = `查询任务${CORE.MAP.ASSET}列表`) {
    const DTO = CORE.grpcDTO(ListClass, param, COM)
    const resp = await CORE.grpc(TIP, CLIENT, 'queryPlanTaskAssetList', DTO)
    return [resp.comQuery.totalCount, resp.planTaskAssetDto || []]
  },
  transList(arr, { rootState: { one: { id } } }) {
    arr.forEach((item) => {
      item.planTaskId = id
    })
    return arr
  },

  // —————————————————————————————— 单项 —————————————————————————————— //

  validOne(one) {
    return one.planTaskId
  },
  async getOne(one, TIP = `查询任务${CORE.MAP.ASSET}详情`) {
    const DTO = CORE.grpcDTO(OneClass, one)
    const resp = await CORE.grpc(TIP, CLIENT, 'queryPlanTaskAssetDetail', DTO)
    const { planTaskAssetDto, taskAssetFileDto, taskAssetItemResultDto } = resp
    return { ...planTaskAssetDto, taskAssetFileDto, taskAssetItemResultDto }
  },
  transOne(obj) {
    obj.taskAssetItemResultDto.forEach((item, i) => {
      item.index = i + 1
      item.id = item.id || item.index
      item.image = item.taskAssetItemFileDto.map((file) => file.fileUrl)
    })
    return obj
  },
}
